﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic.Controller;
using JScripter;
using DataEntity;
using BusinessLogic;

public partial class Admin_AjaxPages_FeatureAdd : AjaxPage
{
    private Guid ID
    {
        get
        {
            if (Request.Params["fid"] != null || AjaxState.ContainsKey("fid"))
            {
                if (!AjaxState.ContainsKey("fid"))
                    AjaxState["fid"] = Request.Params["fid"]; 
                
                return new Guid(AjaxState["fid"]);
            }
            else
            {
                return Guid.Empty;
            }
        }
    }
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        if (ID != Guid.Empty)
        {
            BindData();
        }
    }

    private void AddData()
    {
        try
        {
            Feature objFeature = new Feature();

            objFeature.FeatureID = Guid.NewGuid();

            objFeature.Name = HtmlHelper.ControlValue(txtName.ClientID);

            objFeature.ModifiedDate = DateTime.Now;

            new FeatureController().Add(objFeature);
            Response.Redirect("~/Admin/AjaxPages/FeatureList.aspx");
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = "<div class='error'>" + ex.Message + "</div>";
        }
    }

    private void EditData()
    {

        Feature objFeature = new Feature();

        objFeature.FeatureID = ID;

        objFeature.Name = HtmlHelper.ControlValue(txtName.ClientID);

        objFeature.ModifiedDate = DateTime.Now;

        new FeatureController().UpdateByFeatureID(objFeature);
        Response.Redirect("~/Admin/AjaxPages/FeatureList.aspx");
    }

    private void BindData()
    {
        var dataBunch = new FeatureController().GetbyFeatureID(ID);
        if (dataBunch.Count > 0)
        {
            var data = dataBunch[0];

            

            txtName.Text = data.Name;

            

        }
    }

    protected void AddAjaxClick(object sender, AjaxControl.AjaxEventArg e)
    {
        AddData();
        
    }
    public void PreventData()
    {
        txtName.Text = HtmlHelper.ControlValue(txtName.ClientID);
        

    }

    protected void UpdateAjaxClick(object sender, AjaxControl.AjaxEventArg e)
    {
        EditData();
    }
}